package com.slipper.system.affix.service;

import java.util.List;

import com.slipper.common.util.StringUtil;
import com.slipper.system.affix.dao.AffixItemDao;
import com.slipper.system.api.affix.bo.AffixItemBO;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import com.slipper.common.transaction.ReadTransactional;
import com.slipper.common.transaction.WriteTransactional;
import com.slipper.common.util.Tool;

@Service
@ReadTransactional
public class AffixItemService {
    @Resource
    private AffixItemDao affixItemDao;

    public AffixItemBO queryBy(String id) {
        return affixItemDao.queryBy(id);
    }

    @WriteTransactional
    public AffixItemBO save(AffixItemBO bo) {
        if (StringUtil.isNotBlank(bo.getAffixId())) {
            bo.setOrdinal(affixItemDao.queryMaxOrdinal(bo.getAffixId()) + 1);
        }
        if (bo.getId() != null && !"".equals(bo.getId())) {
            this.affixItemDao.update(bo);
        } else {
            bo.setId(Tool.getUUID());
            this.affixItemDao.insert(bo);
        }
        return bo;
    }

    @WriteTransactional
    public Integer delete(String id) {
        return affixItemDao.delete(id);
    }

    @WriteTransactional
    public Integer deleteAll(String[] ids) {
        int result = 0;
        for (String id : ids) {
            result += affixItemDao.delete(id);
        }
        return result;
    }

    public List<AffixItemBO> queryList(AffixItemBO bo) {
        return this.affixItemDao.queryList(bo);
    }

    public AffixItemBO queryByAffixId(String affixId) {
        return affixItemDao.queryByAffixId(affixId);
    }

}
